package com.webdemo12.dao.impl;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.webdemo12.dao.UserDao;
import com.webdemo12.pojo.User;
@Repository("userDao")
@Transactional
public class UserDaoImpl implements UserDao {
	@Resource
	SessionFactory sessionFactory;
	public void addUser(User user) {
		sessionFactory.getCurrentSession().save(user);
	}
	public void deleteUser(User user) {
		sessionFactory.getCurrentSession().delete(user);
	}
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().update(user);
	}
	public User getUser(int id) {
		User user = (User) sessionFactory.getCurrentSession().get(User.class, id);
		return user;
	}
	@SuppressWarnings("unchecked")
	public List<User> getUsers() {
		List<User> users = sessionFactory.getCurrentSession().createQuery("from User").list();
		return users;
	}
	public User getUser(String name){
		User user = (User) sessionFactory.getCurrentSession().createQuery("from User where name=?").setString(0, name).uniqueResult();
		return user;
	}
}
